﻿using Dabr.NET.Browsers;
using System;
using System.Collections.Generic;

namespace Dabr.NET.Browsers
{
    public abstract class BrowserTheme
    {
        #region Static helpers
        #region User agent filtering strings
        protected static readonly string[] TextUserAgents = { "SIE-MC60" };
        protected static readonly string[] TouchUserAgents =
        {
            "Android",
            "iPhone",
            "Maemo",
            "5800",
            "NokiaN97"
        };
        protected static readonly string[] BlackBerryUserAgents = { "BlackBerry" };
        protected static readonly string[] DesktopUserAgents1 = { "N95" };
        protected static readonly string[] MobileUserAgents =
        {
            "IEMobile",
            "Symbian",
            "SAMSUNG"
        };
        protected static readonly string[] DesktopUserAgents2 =
        {
            "Opera Mini",
            "Opera Mobi",
            "Firefox",
            "Chrome",
            "Opera",
            "MSIE",
            "Safari"
        };
        #endregion
/* The usage of "foreach" in the function ObtainTheme causes warning CS0162.
 * Unfortunately, code being unreachable after finding an appropriate
 * user agent is an EXPECTED behaviour, and thus the warning is undesired.
 * Therefore, we suppress CS0162 only for this function. */
#pragma warning disable 0162
        public static string ObtainTheme(string user_agent)
        {
            // Looks for agents requiring text theme.
            foreach (string s in TextUserAgents)
                if (user_agent.Contains(s))
                    return "text";
            // Looks for agents requiring touch theme.
            foreach (string s in TouchUserAgents)
                if (user_agent.Contains(s))
                    return "touch";
            // Looks for agents requiring BlackBerry-exclusive theme.
            foreach (string s in TouchUserAgents)
                if (user_agent.Contains(s))
                    return "blackberry";
            // Looks for the first set of agents requiring desktop theme.
            foreach (string s in DesktopUserAgents1)
                if (user_agent.Contains(s))
                    return "desktop";
            // Looks for the set of agents requiring mobile theme.
            foreach (string s in MobileUserAgents)
                if (user_agent.Contains(s))
                    return "mobile";
            // Looks for the second set of agents requiring desktop theme.
            foreach (string s in DesktopUserAgents2)
                if (user_agent.Contains(s))
                    return "desktop";
            // Default fallback is the desktop theme. Return it.
            return "desktop";
        }
#pragma warning restore 0162 // Ends disabling warning.

        public static BrowserTheme FindBrowser(string browser)
        {
            throw new NotImplementedException();
        }
        #endregion

        protected DabrPage caller;

        #region Prototypes of required functions
        public abstract string ThemeStatusForm(string text, string in_reply_to_id);
        public abstract string SearchForm(string query);
        #endregion
        #region Derived prototypes
        public string ThemeStatusForm(string text)
        {
            return ThemeStatusForm(text, null);
        }
        public string ThemeStatusForm()
        {
            return ThemeStatusForm("", null);
        }
        #endregion
    }
}

namespace Dabr.NET
{
    public partial class DabrPage : System.Web.UI.Page
    {
        BrowserTheme current_theme;
        protected void DetectBrowser()
        {
            string _browser = "desktop"; // Default fallback is "desktop".
            try
            {
                // TODO: Uncomment below after implementing FetchSetting
                //_browser = FetchSetting("browser");
                // TODO: Remove below after implementing FetchSetting
                throw new KeyNotFoundException();
            }
            catch (KeyNotFoundException) // Browser setting not found.
            {
                if (Request.Headers["X-Nokia-BEARER"] == "GPRS")
                    _browser = "text";
                else
                {
                    string _useragent = Request.Headers["X-Device-User-Agent"];
                    if (_useragent == null)
                        _useragent = Request.UserAgent;
                    _browser = BrowserTheme.ObtainTheme(_useragent);
                }
            }
            catch (Exception e) // Nothing else from FetchString is actually expected.
            {
                throw e;
            }
            // KeyNotFoundException(expected behaviour) is successfully caught.
            current_theme = BrowserTheme.FindBrowser(_browser);
        }
    }
}